<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/30
 * Time: 16:58
 */

namespace app\api\controller\v1;


use app\common\lib\Aes;
use app\common\lib\IAuth;

class User extends AuthBase
{

    /**
     * 获取用户信息
     */
    public function read()
    {
        $obj = new Aes();
        return show(config('code.success'), 'OK', $obj->encrypt($this->user), 200);
    }


    /**
     * 更新用户信息
     * @return array
     */
    public function update(){
        $postData = input('param.');
        // validate  进行校验

        $data = [];
        if (!empty($postData['image'])){
            $data['image'] = $postData['image'];
        }
        if (!empty($postData['username'])){
            $data['username'] = $postData['username'];
        }
        if (!empty($postData['sex'])){
            $data['sex'] = $postData['sex'];
        }
        if (!empty($postData['signature'])){
            $data['signature'] = $postData['signature'];
        }

        if (!empty($postData['password'])){
            //数据传输过程中需要 Aes 加密
            $pass = IAuth::encrypt($postData['password']);
            $data['password'] = $pass['pass'];
            $data['encrypt'] = $pass['rand'];
        }

        if (empty($data)){
            return show(config('code.error'), '数据不合法', []. 404);
        }

        try{
            $id = model('User')->save($data, ['id' => $this->user->id]);
            if ($id){
                return show(config('code.success'), 'OK', [], 202);
            }else{
                return show(config('code.error'), '更新失败', [], 403);
            }
        }catch (\Exception $e){
            return show(config('code.error'), $e->getMessage(), [], 500 );
        }

    }

}